/*
 * libbeluga
 *
 * libbeluga is the property of its developers.  See the COPYRIGHT file
 * for more details.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "libbeluga.h"
#include "BelugaAccount.h"
#include "BelugaPod.h"
#include "BelugaProtocol.h"
#include "BelugaUpdate.h"
#include "beluga_connection.h"

#define BELUGA_AVAILABLE  "ONLINE"
#define BELUGA_IMAGES_DIR "plugins/purple-beluga/images"
#define BELUGA_POD_IMAGES_DIR "plugins/purple-beluga/podimg"

/******************************************************************************/
/* PRPL functions */
/******************************************************************************/

static const char *
beluga_list_icon(PurpleAccount *account, PurpleBuddy *buddy)
{
  return "beluga";
}


static GList *
beluga_statuses(PurpleAccount *account)
{
  GList *types = NULL;
  PurpleStatusType *status;
  
  purple_debug_info("beluga", "statuses\n");
  
  // Beluga pods are either online or offline
  status = purple_status_type_new_full(PURPLE_STATUS_AVAILABLE, NULL, NULL, TRUE, TRUE, FALSE);
  types = g_list_append(types, status);
  
  status = purple_status_type_new_full(PURPLE_STATUS_OFFLINE, NULL, NULL, TRUE, TRUE, FALSE);
  types = g_list_append(types, status);
  
  purple_debug_info("beluga", "statuses return\n");
  return types;
}

static void
beluga_action_check_version_cb(PurplePluginAction *action)
{
  PurpleConnection *pc;
  BelugaAccount *account;

  pc = (PurpleConnection *)action->context;
  account = (BelugaAccount *)pc->proto_data;
  PurpleBelugaCheckVersion(account);
}

static GList *
plugin_actions(PurplePlugin *plugin, gpointer context)
{
  GList *list = NULL;
  PurplePluginAction *action = NULL;

  action = purple_plugin_action_new("Check for Software Updates", 
				    beluga_action_check_version_cb);
  action->context = context;
  list = g_list_append(list, action);

  return list;
}

static void 
beluga_login(PurpleAccount *account)
{
  purple_debug_info("beluga", "login\n");
  BelugaConnect(account);
}

static void 
beluga_close(PurpleConnection *pc)
{
  BelugaAccount *account;
  purple_debug_info("beluga", "close\n");
  account = pc->proto_data;
  BelugaDisconnect(account);
}

static int
beluga_send_msg(PurpleConnection *pc, 
		const char *who, const char *msg,
		PurpleMessageFlags flags)
{
  BelugaAccount *account;
  
  purple_debug_info("beluga", "send_msg(%s <= %s\n",
		    who, msg);
  account = pc->proto_data;
  BelugaSendMessage(account, who, msg);
  if (flags & PURPLE_MESSAGE_INVISIBLE) {
    return 0;
  }
  return 0;
}

#if PURPLE_MAJOR_VERSION >= 2 && PURPLE_MINOR_VERSION >= 5
static GHashTable *
beluga_get_account_text_table(PurpleAccount *account)
{
  GHashTable *table;
  
  table = g_hash_table_new(g_str_hash, g_str_equal);
  
  g_hash_table_insert(table, "login_label", (gpointer)_("Email Address..."));
  
  return table;
}
#endif

/******************************************************************************/
/* Plugin functions */
/******************************************************************************/

static gboolean plugin_load(PurplePlugin *plugin)
{
  belugaPlugin = plugin;
  return TRUE;
}

static gboolean plugin_unload(PurplePlugin *plugin)
{
  return TRUE;
}

static void plugin_init(PurplePlugin *plugin)
{
  PurpleAccountOption *option;
  PurplePluginInfo *info = plugin->info;
  PurplePluginProtocolInfo *prpl_info = info->extra_info;
  
  option = purple_account_option_string_new("Host", "host", "belugapods.com");
  prpl_info->protocol_options = g_list_append(prpl_info->protocol_options, option);
  
  option = purple_account_option_int_new("Initial Messages fetched", "seedFetchCount", 30);
  prpl_info->protocol_options = g_list_append(prpl_info->protocol_options, option);
  
  option = purple_account_option_string_new("Display Photo Link as", "photo", "(Photo)");
  prpl_info->protocol_options = g_list_append(prpl_info->protocol_options, option);
  
  option = purple_account_option_string_new("Display Location Link as", "location", "(*)");
  prpl_info->protocol_options = g_list_append(prpl_info->protocol_options, option);
  
  option = purple_account_option_bool_new("Automatically Check for Software Updates", "checkVersion", TRUE);
  prpl_info->protocol_options = g_list_append(prpl_info->protocol_options, option);

  option = purple_account_option_bool_new("Display Debug Messages", "debugMsgs", FALSE);
  prpl_info->protocol_options = g_list_append(prpl_info->protocol_options, option);
  /*
  option = purple_account_option_string_new("debug 1", "debug1", "");
  prpl_info->protocol_options = g_list_append(prpl_info->protocol_options, option);
  
  option = purple_account_option_string_new("debug 2", "debug2", "");
  prpl_info->protocol_options = g_list_append(prpl_info->protocol_options, option);
  */
}

static PurplePluginProtocolInfo prpl_info = {
  /* options */
  0,
  
  NULL,                   /* user_splits */
  NULL,                   /* protocol_options */
  NO_BUDDY_ICONS          /* icon_spec */
  /*{"jpg", 0, 0, 50, 50, -1, PURPLE_ICON_SCALE_SEND}*/, /* icon_spec */
  beluga_list_icon,       /* list_icon */
  NULL,                   /* list_emblems */
  NULL,                   /* status_text */
  NULL,                   /* tooltip_text */
  beluga_statuses,        /* status_types */
  NULL,                   /* blist_node_menu */
  NULL,                   /* chat_info */
  NULL,                   /* chat_info_defaults */
  beluga_login,           /* login */
  beluga_close,           /* close */
  beluga_send_msg,        /* send_im */
  NULL,                   /* set_info */
  NULL,                   /* send_typing */
  NULL,                   /* get_info */
  NULL,                   /* set_status */
  NULL,                   /* set_idle */
  NULL,                   /* change_passwd */
  NULL,                   /* add_buddy */
  NULL,                   /* add_buddies */
  NULL,                   /* remove_buddy */
  NULL,                   /* remove_buddies */
  NULL,                   /* add_permit */
  NULL,                   /* add_deny */
  NULL,                   /* rem_permit */
  NULL,                   /* rem_deny */
  NULL,                   /* set_permit_deny */
  NULL,                   /* join_chat */
  NULL,                   /* reject chat invite */
  NULL,                   /* get_chat_name */
  NULL,                   /* chat_invite */
  NULL,                   /* chat_leave */
  NULL,                   /* chat_whisper */
  NULL,                   /* chat_send */
  NULL,                   /* keepalive */
  NULL,                   /* register_user */
  NULL,                   /* get_cb_info */
  NULL,                   /* get_cb_away */
  NULL,                   /* alias_buddy */
  NULL,                   /* group_buddy */
  NULL,                   /* rename_group */
  NULL,                   /* buddy_free */
  NULL,                   /* convo_closed */
  purple_normalize_nocase,/* normalize */
  NULL,                   /* set_buddy_icon */
  NULL,                   /* remove_group */
  NULL,                   /* get_cb_real_name */
  NULL,                   /* set_chat_topic */
  NULL,                   /* find_blist_chat */
  NULL,                   /* roomlist_get_list */
  NULL,                   /* roomlist_cancel */
  NULL,                   /* roomlist_expand_category */
  NULL,                   /* can_receive_file */
  NULL,                   /* send_file */
  NULL,                   /* new_xfer */
  NULL,                   /* offline_message */
  NULL,                   /* whiteboard_prpl_ops */
  NULL,                   /* send_raw */
  NULL,                   /* roomlist_room_serialize */
  NULL,                   /* unregister_user */
  NULL,                   /* send_attention */
  NULL,                   /* attention_types */
#if PURPLE_MAJOR_VERSION >= 2 && PURPLE_MINOR_VERSION >= 5
  sizeof(PurplePluginProtocolInfo), /* struct_size */
  beluga_get_account_text_table, /* get_account_text_table */
#else
  (gpointer) sizeof(PurplePluginProtocolInfo)
#endif
};

static PurplePluginInfo plugin_info = {
  PURPLE_PLUGIN_MAGIC,
  PURPLE_MAJOR_VERSION,
  PURPLE_MINOR_VERSION,
  PURPLE_PLUGIN_PROTOCOL, /* type */
  NULL, /* ui_requirement */
  0, /* flags */
  NULL, /* dependencies */
  PURPLE_PRIORITY_DEFAULT, /* priority */
  "prpl-battleactionken-beluga", /* id */
  "Beluga", /* name */
  BELUGA_PLUGIN_VERSION, /* version */
  N_("Beluga Protocol Plugin"), /* summary */
  N_("Beluga Protocol Plugin"), /* description */
  "Ken Patel <kpatelPro@gmail.com>", /* author */
  "http://code.google.com/p/purple-beluga/", /* homepage */
  plugin_load, /* load */
  plugin_unload, /* unload */
  NULL, /* destroy */
  NULL, /* ui_info */
  &prpl_info, /* extra_info */
  NULL, /* prefs_info */
  plugin_actions, /* actions */
  
  /* padding */
  NULL,
  NULL,
  NULL,
  NULL
};

PURPLE_INIT_PLUGIN(beluga, plugin_init, plugin_info);
